--#include "data\config\boss\BossSoulConfig.lua" once 				--兽魂配置（点亮珠子）
--#include "data\config\boss\NewBossSoulConfig.lua" once 			--兽魂配置（抽取兽魂）
--#include "data\functions\Common\CommonFunc.lua" once				--一些通用方法

BOSS_SOUL_TYPE_MAX = 6			--最大兽魂

--[[下发兽魂珠子数据
消息(49,6)
]]
function SendBossSoulBeadData( sysarg )
	local npack = DataPack.allocPacket(sysarg, enBossSystemID, sSendBossSoulData)
	if npack then
		local bossSoulIdx, bossSoulBeadIdx = Actor.getBossSoulBead( sysarg, bossSoulIdx, bossSoulBeadIdx )
		DataPack.writeShort( npack, bossSoulIdx )
		DataPack.writeShort( npack, bossSoulBeadIdx )
		DataPack.flush(npack)
		--print("SendBossSoulData, bossSoulIdx="..bossSoulIdx..", bossSoulBeadIdx="..bossSoulBeadIdx)
	end

	--local bossScore = Actor.getUIntProperty(sysarg, PROP_ACTOR_BOSS_SCORE)
	--print("SendBossSoulData, bossScore="..bossScore)
end

--[[
点开下一个兽魂珠子
消息(49,7)
PROP_ACTOR_BOSS_SCORE,									//（125）BOSS积分
]]
function OpenBossSoulBead( sysarg )
	local bossSoulIdx, bossSoulBeadIdx = Actor.getBossSoulBead( sysarg, bossSoulIdx, bossSoulBeadIdx )
	if bossSoulIdx <= 0 then
		bossSoulIdx = 1				--从第1个兽魂开始
	end
	--print("OpenBossSoulBead, bossSoulIdx="..bossSoulIdx..", bossSoulBeadIdx="..bossSoulBeadIdx)
	local soulCfg = BossSoulConfig[bossSoulIdx]
	if not soulCfg then
		return
	end

	if bossSoulIdx >= #BossSoulConfig  and bossSoulBeadIdx >= #soulCfg.soulBeads then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.BossSoul001,ttFlyTip)
		return
	end

	local nextBossSoulIdx 		= 0
	local nextBossSoulBeadIdx	= 0
	if bossSoulIdx == #BossSoulConfig then
		if bossSoulBeadIdx < #soulCfg.soulBeads then
			nextBossSoulIdx 	= bossSoulIdx
			nextBossSoulBeadIdx	= bossSoulBeadIdx + 1
		else
			return
		end
	elseif bossSoulIdx < #BossSoulConfig then
		if bossSoulBeadIdx < #soulCfg.soulBeads then
			nextBossSoulIdx 	= bossSoulIdx
			nextBossSoulBeadIdx	= bossSoulBeadIdx + 1
		else
			nextBossSoulIdx 	= bossSoulIdx + 1
			nextBossSoulBeadIdx	= 1
		end
	end


	local nextSoulCfg = BossSoulConfig[nextBossSoulIdx]
	if not nextSoulCfg then
		return
	end

	local nextBeadCfg = nextSoulCfg.soulBeads[nextBossSoulBeadIdx]
	if not nextBeadCfg then
		return
	end

	local openConsumes = nextBeadCfg.consumes
	if not CheckConsumeCond( sysarg, openConsumes, 1 ) then						--一般扣除BOSS积分
		return
	end

	if not DoConsumeCond( sysarg, openConsumes, 1, GameLog.clBossSoulOpenBeadFee, Lang.ScriptTips.BossSoulLog01 ) then
		return
	end

	--print("OpenBossSoulBead, nextBossSoulIdx="..nextBossSoulIdx..", nextBossSoulBeadIdx="..nextBossSoulBeadIdx)
	Actor.setBossSoulBead( sysarg, nextBossSoulIdx, nextBossSoulBeadIdx )
	SendBossSoulData( sysarg )

	--aAchieveBossSoulLevel = 112,	//兽魂 参数1表示兽魂类别(1-第1个兽魂)  参数2表示兽魂珠类别(1-第1个珠子)
	Actor.triggerAchieveEvent(sysarg, 112, nextBossSoulIdx, nextBossSoulBeadIdx)
end

----------------------------------------新兽魂-------------------------------------------------------------------
--[[
角色登陆，将兽魂值传入引擎
]]
function OnBossSoulLogin( sysarg )
	--local actorId = Actor.getActorId( sysarg )
	--print("OnBossSoulLogin, actorId="..actorId)
	local sVar = Actor.getStaticVar(sysarg)
	if sVar.bossSoul then
		for soulType=1, BOSS_SOUL_TYPE_MAX do
			if sVar.bossSoul[soulType] then
				local soulLevel = sVar.bossSoul[soulType][1] or 0
				local soulExp 	= sVar.bossSoul[soulType][2] or 0 
				Actor.setBossSoul( sysarg, soulType, soulLevel, soulExp )
				--print("  -->OnBossSoulLogin, soulType="..soulType..", soulLevel="..soulLevel..", soulExp="..soulExp)
			end
		end
	end
	Actor.collectOperate(sysarg, 0)						--刷属性
end

--[[
角色下线，存储到静态变量中
]]
function OnBossSoulLogout( sysarg )
	--local actorId = Actor.getActorId( sysarg )
	--print("OnBossSoulLogout, actorId="..actorId)
	local sVar = Actor.getStaticVar(sysarg)
	if not sVar.bossSoul then
		sVar.bossSoul = {}
	end

	for soulType=1, BOSS_SOUL_TYPE_MAX do
		local soulLevel, soulExp = Actor.getBossSoul(sysarg, soulType, 0, 0)
		sVar.bossSoul[soulType] = {}
		sVar.bossSoul[soulType][1] = soulLevel
		sVar.bossSoul[soulType][2] = soulExp
		--print("  -->OnBossSoulLogout, soulType="..soulType..", soulLevel="..sVar.bossSoul[soulType][1]..", soulExp="..sVar.bossSoul[soulType][2])
	end

	----记录日志
	local actorName 	= Actor.getName( sysarg )
	local actorId 		= Actor.getActorId( sysarg )
	local soulLevel1 	= sVar.bossSoul[1][1] or 0
	local soulLevel2 	= sVar.bossSoul[2][1] or 0
	local soulLevel3 	= sVar.bossSoul[3][1] or 0
	local soulLevel4 	= sVar.bossSoul[4][1] or 0
	local soulLevel5 	= sVar.bossSoul[5][1] or 0
	local soulLevel6 	= sVar.bossSoul[6][1] or 0
	System.sendCommonLog( GameLog.clLogoutBossSoul, actorId, actorName, soulLevel1, soulLevel2, soulLevel3,
		tostring(soulLevel4), tostring(soulLevel5),  tostring(soulLevel6) )
	--print("OnBossSoulLogout, soulLevel1="..soulLevel1..", soulLevel2="..soulLevel2..", soulLevel3="..soulLevel3..
	--	", soulLevel4="..soulLevel4..", soulLevel5="..soulLevel5..", soulLevel6="..soulLevel6)
end

function SendAllBossSoul( sysarg )
	local npack = DataPack.allocPacket(sysarg, enBossSystemID, sBossSoulSendData)
	if npack then
		DataPack.writeChar(npack, BOSS_SOUL_TYPE_MAX)
		for soulType=1, BOSS_SOUL_TYPE_MAX do
			local soulLevel, soulExp = Actor.getBossSoul(sysarg, soulType, 0, 0)
			DataPack.writeChar(npack, soulType)
			DataPack.writeChar(npack, soulLevel)
			DataPack.writeUInt(npack, soulExp)
			--print("SendAllBossSoul, soulType="..soulType..", soulLevel="..soulLevel..", soulExp="..soulExp)
		end
		DataPack.flush(npack)
	end
end

function SendOneBossSoul( sysarg, soulType )
	local npack = DataPack.allocPacket(sysarg, enBossSystemID, sBossSoulSendData)
	if npack then
		DataPack.writeChar(npack, 1)
		local soulLevel, soulExp = Actor.getBossSoul(sysarg, soulType, 0, 0)
		DataPack.writeChar(npack, soulType)
		DataPack.writeChar(npack, soulLevel)
		DataPack.writeUInt(npack, soulExp)
		--print("SendOneBossSoul, soulType="..soulType..", soulLevel="..soulLevel..", soulExp="..soulExp)

		DataPack.flush(npack)
	end
end

--[[
抽取兽魂
]]
function DrawBossSoul( sysarg )
	if not CheckActorDailyDoneTimes( sysarg, enDailyOpTimes_DrawBossSoul, 0, 1 ) then
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0021,ttFlyTip)
		return
	end

	local drawTimes = GetActorDailyDoneTimes( sysarg, enDailyOpTimes_DrawBossSoul, 0 )
	--print("DrawBossSoul, drawTimes="..drawTimes)
	local nextTimes = drawTimes + 1
	local drawConsumes = NewBossSoulConfig.drawSoulConsume[nextTimes]
	if not drawConsumes then
		drawConsumes = NewBossSoulConfig.drawSoulConsume[#NewBossSoulConfig.drawSoulConsume]			--消耗取最后一个
	end

	if not drawConsumes then 		--没有对应的消耗
		Actor.sendTipmsg(sysarg, Lang.ScriptTips.COM0021,ttFlyTip)
		return
	end

	if not CheckConsumeCond( sysarg, drawConsumes, 1 ) then
		return
	end

	if not DoConsumeCond( sysarg, drawConsumes, 1, GameLog.clBossSoulDrawFee, Lang.ScriptTips.BossSoulLog02 ) then
		return
	end

	AddActorDailyDoneTimes( sysarg, enDailyOpTimes_DrawBossSoul, 0, 1 )

	local idx, item = GetItemIdxRand( NewBossSoulConfig.drawSoul )
	if not idx or not item then
		return
	end

	AddBossSoul( sysarg, item.soulType, item.addSoulExp )
end

--[[
增加兽魂经验
]]
function AddBossSoul( sysarg, soulType, addSoulExp )
	local soulLevel, soulExp = Actor.getBossSoul(sysarg, soulType, 0, 0)
	--print("AddBossSoul, soulType="..soulType..", addSoulExp="..addSoulExp..", soulLevel="..soulLevel..", soulExp="..soulExp)
	local soulCfg 	= NewBossSoulConfig.soulLevel[soulType]
	if not soulCfg then
		return
	end

	local newSoullevel, newSoulExp = SetBossSoul( soulCfg, soulLevel, soulExp + addSoulExp )
	--[[
	local nextSoulCfg 	= soulCfg[soulLevel+1]
	if nextSoulCfg then 			--该兽魂未满
		--print("AddBossSoul, nextSoulLevel="..(soulLevel + 1)..", needSoulExp="..nextSoulCfg.needSoulExp)
		if newSoulExp >= nextSoulCfg.needSoulExp then 			--可以升级
			newSoullevel	= soulLevel + 1
			newSoulExp 		= newSoulExp - nextSoulCfg.needSoulExp
			Actor.collectOperate(sysarg, 0)						--兽魂等级变化，刷属性
		end
	end
	]]
	Actor.setBossSoul( sysarg, soulType, newSoullevel, newSoulExp )
	Actor.collectOperate(sysarg, 0)								--兽魂等级变化，刷属性
	SendOneBossSoul( sysarg, soulType )

	--aAchieveBossSoulLevel = 112,	//兽魂 参数1表示兽魂类别(1-第1个兽魂)  参数2表示兽魂珠类别(1-第1个珠子)
	Actor.triggerAchieveEvent(sysarg, aAchieveBossSoulLevel, soulType, newSoullevel)
end

function SetBossSoul( soulCfg, soulLevel, soulExp )
	local nextSoulCfg = soulCfg[soulLevel+1]
	if not nextSoulCfg then
		return soulLevel, soulExp
	end
	local needSoulExp = nextSoulCfg.needSoulExp
	--print("-->SetBossSoul, soulLevel="..soulLevel..", soulExp="..soulExp..", needSoulExp="..needSoulExp)
	if soulExp < needSoulExp then
		return soulLevel, soulExp
	else
		return SetBossSoul( soulCfg, soulLevel+1, soulExp-needSoulExp )
	end
end

function GmSetBossSoul( sysarg, soulType, soulLevel, soulExp )
	--print("GmSetBossSoul, soulType="..soulType..", soulLevel="..soulLevel..", soulExp="..soulExp)
	Actor.setBossSoul( sysarg, soulType, soulLevel, soulExp )
	Actor.collectOperate(sysarg, 0)								--兽魂等级变化，刷属性
	SendOneBossSoul( sysarg, soulType )
end